package com.graduationdesign.zengyan.graduationdesignapp.ui.presenter;

import com.graduationdesign.zengyan.graduationdesignapp.BaseApplication;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.AccountManager;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.Organization;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.profileRepository.DataGenerator;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.profileRepository.MainService;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseLoadingView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BasePresenter;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.Transforms;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.ProfileContact;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by ZengYan on 2018/4/24.
 *
 * @desc :
 */
public class ProfilePresenter extends BasePresenter implements ProfileContact.Presenter {


    public ProfileContact.ShowPeopleInfo showPeopleInfo;
    public ProfileContact.ShowAllEvents showAllEvents;
    public ProfileContact.ShowEventsISignUp showEventsISignUp;
    public ProfileContact.ShowSignUpResult showSignUpResult;
    public ProfileContact.ShowOrgsIJoin showOrgsIJoin;
    public ProfileContact.ShowSearchUsers showSearchUsers;
    public ProfileContact.ShowRecentlyUsed showRecentlyUsed;


    private MainService mainService;
    private BaseLoadingView mLoadingView;

    private ProfilePresenter(Builder builder) {
        super(builder.mBaseErrorView);
        showPeopleInfo = builder.showPeopleInfo;
        showAllEvents = builder.showAllEvents;
        showEventsISignUp = builder.showEventsISignUp;
        showSignUpResult = builder.showSignUpResult;
        showOrgsIJoin = builder.showOrgsIJoin;
        showSearchUsers = builder.showSearchUsers;
        showRecentlyUsed = builder.showRecentlyUsed;
        mainService = builder.mainService;
        mLoadingView = builder.mLoadingView;
    }


    public static Builder newBuilder() {
        return new Builder();
    }

    @Override
    protected String provideTag() {
        return "ProfilePresenter";
    }

    @Override
    public void loadOrgsIJoinIn(String stunum, BaseLoadingView loadingView) {
        mainService.getMyOrgs(stunum)
                .compose(Transforms.applyAll(mLoadingView, compositeDisposable))
                .subscribe(listResult -> {
                    List<Organization> organizations = DataGenerator.getOrganization();
                    listResult.getData().addAll(organizations);
                    showOrgsIJoin.showOrgsIJoin(listResult.getData());
                }, mErrorConsumer);
    }

    @Override
    public void loadPeople(String stunum, BaseLoadingView loadingView) {
        mainService.getProfileInfo(stunum)
                .compose(Transforms.applyAll(loadingView, compositeDisposable))
                .subscribe(userResult -> {
                    if (showPeopleInfo == null) {
                        return;
                    }
                    showPeopleInfo.showPeopleInfo(userResult.getData());
                }, mErrorConsumer);
    }

    @Override
    public void signUp(String stunum, int event_id) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("stuNum", stunum);
        map.put("eventId", String.valueOf(event_id));

        mainService.signUp(map)
                .compose(Transforms.applyAll(mLoadingView, compositeDisposable))
                .subscribe(voidResult -> {
                    showSignUpResult.showSignUpResult(voidResult);
                }, mErrorConsumer);

    }

    @Override
    public void loadEventsISignUp(String stunum) {
        mainService.getEventsIJoinUp(stunum)
                .compose(Transforms.applyAll(mLoadingView, compositeDisposable))
                .subscribe(listResult -> {
                    showEventsISignUp.showEventsISignUp(listResult.getData());
                }, mErrorConsumer);

    }

    @Override
    public void loadAllEvents() {
        mainService.getEventsAll(AccountManager.getInstance().getCurrentStuNum())
                .compose(Transforms.applyAll(mLoadingView, compositeDisposable))
                .subscribe(listResult -> {
                    showAllEvents.showAllEvents(listResult.getData());
                }, mErrorConsumer);
    }

    @Override
    public void searchUser(String stunum, String message) {
        mainService.searchUser(stunum, message)
                .compose(Transforms.applyAll(mLoadingView, compositeDisposable))
                .subscribe(listResult -> {
                    showSearchUsers.showSearchUsers(listResult.getData());
                }, mErrorConsumer);
    }

    @Override
    public void loadRecentlyUsed() {
        Observable.just(1)
                .subscribeOn(Schedulers.io())
                .map(integer -> {
                    return BaseApplication.mDatabase.getRecentlyUsedDao().getAll();
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(recentlyUseds -> {
                    showRecentlyUsed.showRecentlyUsed(recentlyUseds);
                },mErrorConsumer);
    }


    public static final class Builder {
        private ProfileContact.ShowPeopleInfo showPeopleInfo;
        private ProfileContact.ShowAllEvents showAllEvents;
        private ProfileContact.ShowEventsISignUp showEventsISignUp;
        private ProfileContact.ShowSignUpResult showSignUpResult;
        private ProfileContact.ShowOrgsIJoin showOrgsIJoin;
        private ProfileContact.ShowSearchUsers showSearchUsers;
        private ProfileContact.ShowRecentlyUsed showRecentlyUsed;

        private BaseErrorView mBaseErrorView;
        private MainService mainService;
        private BaseLoadingView mLoadingView;

        private Builder() {

        }

        public Builder setBaseErrorView(BaseErrorView val) {
            mBaseErrorView = val;
            return this;
        }

        public Builder setShowPeopleInfo(ProfileContact.ShowPeopleInfo val) {
            showPeopleInfo = val;
            return this;
        }

        public Builder setShowAllEvents(ProfileContact.ShowAllEvents val) {
            showAllEvents = val;
            return this;
        }

        public Builder setShowEventsISignUp(ProfileContact.ShowEventsISignUp val) {
            showEventsISignUp = val;
            return this;
        }

        public Builder setShowSignUpResult(ProfileContact.ShowSignUpResult val) {
            showSignUpResult = val;
            return this;
        }

        public Builder setShowOrgsIJoin(ProfileContact.ShowOrgsIJoin val) {
            showOrgsIJoin = val;
            return this;
        }

        public Builder setShowSearchUsers(ProfileContact.ShowSearchUsers val) {
            showSearchUsers = val;
            return this;
        }

        public Builder setShowRecentlyUsed(ProfileContact.ShowRecentlyUsed val) {
            showRecentlyUsed = val;
            return this;
        }


        public Builder setMainService(MainService val) {
            mainService = val;
            return this;
        }

        public Builder setMLoadingView(BaseLoadingView val) {
            mLoadingView = val;
            return this;
        }

        public ProfilePresenter build() {
            return new ProfilePresenter(this);
        }


    }


}
